import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// 初始化时用sessionStore.getItem('token'),这样子刷新页面就无需重新登录
const state = {
  token: window.localStorage.getItem('ycd_token'),
  // token: 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxNTgyIiwiYXV0aCI6IlJPTEVfVVNFUiIsImV4cCI6MTU3MzExMTE0Nn0.Vr5amwS_vKMA10LKlth8uP8o4_enSgUuhEr9MRsB3FGrUnbX-zfINvu2-mGUcWs29a5wSRV8jW3oDSffM5UAyA',
  username: '',
  // 左侧菜单栏数据
  // menuItems: JSON.parse(window.localStorage.getItem('menuItems'))
  menuItems: [
    {
      children: [
        {
          name: 'menuManage',
          text: '菜单管理'
        },
        {
          name: 'roleManage',
          text: '版本管理'
        }
        // {
        //   name: 'accountManage',
        //   text: '账户管理'
        // }
      ],
      icon: 'zhanghaoshezhi',
      text: '权限管理'
    },
    {
      children: [
        {
          name: 'goodsList',
          text: '合同模板列表'
        },
        {
          name: 'addingGoods',
          text: '新增合同'
        }
      ],
      icon: 'hezuofangguanli',
      text: '合同模板管理'
    },
    {
      children: [
        {
          name: 'fleeingGoods',
          text: 'IP列表'
        },
        {
          name: 'codeManagement',
          text: '新增IP'
        }
      ],
      icon: 'biaoqianguanli',
      text: 'IP百科'
    }
  ]
}

const mutations = {
  LOGIN: (state, data) => {
    // 更改token的值
    state.token = data
    window.localStorage.setItem('ycd_token', data)
  },
  LOGOUT: (state) => {
    // 登出的时候要清除token
    state.token = null
    state.username = null
    state.menuItems = null
    window.localStorage.removeItem('ycd_token')
    window.localStorage.removeItem('ycd_username')
    window.localStorage.removeItem('menuItems')
  },
  USERNAME: (state, data) => {
    // 把用户名存起来
    state.username = data
    window.localStorage.setItem('ycd_username', data)
  },
  setMenus: (state, items) => {
    state.menuItems = [...items]
  },
  USERMENU: (state, data) => {
    // 把菜单存起来
    state.menuItems = JSON.parse(data)
    window.localStorage.setItem('menuItems', data)
  }
}

const actions = {
  UserLogin ({commit}, data) {
    commit('LOGIN', data)
  },
  UserLogout ({commit}) {
    commit('LOGOUT')
  },
  UserName ({commit}, data) {
    commit('USERNAME', data)
  },
  UserMenus ({commit}, data) {
    commit('USERMENU', data)
  }
}

export default new Vuex.Store({
  state,
  mutations,
  actions
})
